/**
 * 您的公司.com
 * Copyright (c) 2018-2018 All Rights Reserved.
 */
package com.mei.demo.Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 启动线程
 *
 * @author mei.xiaoliang@qq.com
 */
public class Demo3 {
    public static void main(String[] args) {
        FutureTask<String> task1 = new FutureTask<String>(new threadNew2("线程 1"));
        FutureTask<String> task2 = new FutureTask<String>(new threadNew2("线程 2"));
        FutureTask<String> task3 = new FutureTask<String>(new threadNew2("线程 3"));
        new Thread(task1, "xxxxx").start();
        new Thread(task2, "yyyyy").start();
        new Thread(task3).start();
        try {
            System.out.println(task1.get());
            System.out.println(task2.get());
            System.out.println(task3.get());
        } catch (InterruptedException | ExecutionException e) {
            System.out.println("有毛病");
        }
    }
}

/**
 * 自定义 FutureTask 定义线程
 *
 * @author mei.xiaoliang@qq.com
 */
class threadNew2 implements Callable<String> {

    private String name;

    public threadNew2(String name) {
        this.name = name;
    }

    /** 
     * @see java.util.concurrent.Callable#call()
     */
    @Override
    public String call() throws Exception {
        for (int i = 0; i < 5; i++) {
            System.out.println(name + " 走你: i=" + i);
        }
        System.out.println("currentThread name:" + Thread.currentThread().getName());
        return name + " over";
    }

}